<?php
/**
 * 店铺冻结列表model类
 *
 * @access public
 * @author wangbiao<wangbiao@shopex.cn>
 * @version $Id: shopfreeze.php 2016-03-18 10:00
 */
class console_mdl_shopfreeze extends dbeav_model
{
    public function table_name($real = false){
        if($real){
            $table_name = 'sdb_material_sales_material_shop_freeze';
        }else{
            $table_name = 'sales_material_shop_freeze';
        }
        
        return $table_name;
    }
    
    public function get_schema()
    {
        $db_schema    = app::get('material')->model('sales_material_shop_freeze')->get_schema();
        
        #格式化表结构
        if($db_schema['columns']['sm_id'])
        {
            $db_schema['columns']['sm_id']['label']         = '销售物料编码';
            $db_schema['columns']['sm_id']['searchtype']    = 'nequal';
            $db_schema['columns']['sm_id']['filtertype']    = 'normal';
            $db_schema['columns']['sm_id']['filterdefault'] = true;
        }
        if($db_schema['columns']['shop_id'])
        {
            $db_schema['columns']['shop_id']['label']    = '所属店铺';
        }
        if($db_schema['columns']['shop_freeze'])
        {
            $db_schema['columns']['shop_freeze']['label']  = '店铺冻结数量';
        }
        
        return $db_schema;
    }
    
    public function getList($cols='*', $filter=array(), $offset=0, $limit=-1, $orderby=null)
    {
        if($orderby)
        {
            $orderby    = ' ORDER BY ' . $orderby;
        }
        
        $sql    = "SELECT a.*, b.sales_material_bn, b.sales_material_name FROM ". $this->table_name(true) ." AS a LEFT JOIN sdb_material_sales_material AS b ON a.sm_id=b.sm_id 
                   WHERE ". $this->_filter($filter) . $orderby;
        $rows = $this->db->selectLimit($sql, $limit, $offset);
        
        #格式化数据
        $shopObj    = app::get('ome')->model('shop');
        
        if(!empty($rows))
        {
            foreach ($rows as $key => $val)
            {
                #店铺冻结数量
                $link_url    = 'index.php?app=console&ctl=admin_shopfreeze&act=show_shopfreeze_list&sm_id='. $val['sm_id'] .'&shop_id='. $val['shop_id'];
                $rows[$key]['shop_freeze']    = '<a href="'. $link_url .'" target="_blank" title="点击重新计算店铺冻结数量.">'. $val['shop_freeze'] .'</a>';
                
                #货号
                $rows[$key]['sm_id']    = $val['sales_material_bn'];
                
                #店铺名称
                $shopInfo   = $shopObj->dump(array('shop_id'=>$val['shop_id']), 'name');
                $rows[$key]['shop_id']    = $shopInfo['name'];
            }
        }
        
        return $rows;
    }
    
    public function _filter($filter, $tableAlias=null, $baseWhere=null)
    {
        $where    = array();
        
        if(isset($filter['sm_id']) && $filter['sm_id'])
        {
            $where[] = "b.sales_material_bn='". $filter['sm_id'] ."'";
            unset($filter['sm_id']);
        }
        
        $filter    = parent::_filter($filter, 'a', $baseWhere) . ($where ? " AND " .implode($where, ' AND ') : '');
        
        return $filter;
    }
    
    public function count($filter = null)
    {
        $sql    = "SELECT count(*) as _count FROM ". $this->table_name(true) ." AS a LEFT JOIN sdb_material_sales_material AS b ON a.sm_id=b.sm_id 
                   WHERE ". $this->_filter($filter);
        $rows   = $this->db->select($sql);
        
        return intval($rows[0]['_count']);
    }
}
